package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.DTO.AccountDTO;
import com.briup.VO.AuthGetAcountInfoVO;
import com.briup.constant.SystemConstant;
import com.briup.enums.CodeAndMessageEnum;
import com.briup.exception.SystemException;
import com.briup.mapper.AccountMapper;
import com.briup.pojo.Account;
import com.briup.service.AccountService;
import com.briup.util.BeanCopyUtils;
import com.briup.util.JWTUtils;
import com.briup.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author Lenovo
 * @description 针对表【pts_account(系统用户)】的数据库操作Service实现
 * @createDate 2024-03-11 09:59:35
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
        implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private RedisUtils redisUtils;


    @Override
    public Map<String, String> login(AccountDTO accountDTO) throws SystemException {
//        参数校验
//        用户名或密码不能为空
        if (Objects.isNull(accountDTO)
                || Objects.isNull(accountDTO.getUsername())
                || Objects.isNull(accountDTO.getPassword())
                || !StringUtils.hasText(accountDTO.getPassword())
                || !StringUtils.hasText(accountDTO.getUsername())) {
//            用户名与密码为空
            throw new SystemException(CodeAndMessageEnum.USERNAME_OR_PASSWORD_IS_NOT_NULL);
        }
//        用户名与密码不为空
//        构造条件构造器
        LambdaQueryWrapper<Account> accountExtendLambdaQueryWrapper = new LambdaQueryWrapper<>();
        accountExtendLambdaQueryWrapper.eq(Account::getUsername, accountDTO.getUsername())
                .eq(Account::getPassword, accountDTO.getPassword());
        Account account = accountMapper.selectOne(accountExtendLambdaQueryWrapper);
//        判断
        if (Objects.isNull(account)) {
            throw new SystemException(CodeAndMessageEnum.USERNMAE_OR_PASSWORD_IS_ERROR);
        }
//        存放用户信息
        Map<String, Object> map = new HashMap<>();
//        用户名和密码正确
        map.put("accountId", account.getAccountId());
        map.put("username", account.getUsername());
        map.put("status",account.getStatus());
        String jwt = JWTUtils.generator(map);
        HashMap<String, String> jwtMap = new HashMap<>();
        jwtMap.put("token", jwt);
        // 使用Redis存储用户信息
        redisUtils.set(account.getAccountId(),jwt, SystemConstant.ONE_DAY_CONVERSION_SECOND);
         // 设置过期时间为1小时
        return jwtMap;
    }

    @Override
    public AuthGetAcountInfoVO getAccounts(Integer id) {
        Account account = accountMapper.selectById(id);
//        Bean拷贝
        AuthGetAcountInfoVO authGetAcountInfoVO = BeanCopyUtils.copyBean(account, AuthGetAcountInfoVO.class);
        return authGetAcountInfoVO;
    }

    @Override
    public void logout(String id) {
        redisUtils.del(id);
    }
}




